package com.mxpio.erp.sales.controller;


import com.mxpio.erp.common.sales.entity.RejectBill;
import com.mxpio.erp.common.sales.service.RejectBillService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "RejectBillController", tags = { "退货通知单接口" })
@RestController
@RequestMapping("/erp/sales/rb/")
public class RejectBillController {

    @Autowired
    private RejectBillService service;

    @GetMapping("list")
    @ApiOperation(value = "退货通知单列表", notes = "获取退货通知单列表（无分页）", httpMethod = "GET")
    public Result<List<RejectBill>> list(@ApiParam(name="查询参数")  String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<RejectBill> rejectBills = service.list(RejectBill.class, c);
        return Result.OK(rejectBills);
    }

    @GetMapping("page")
    @ApiOperation(value = "退货通知单列表", notes = "获取退货通知单列表（分页）", httpMethod = "GET")
    public Result<Page<RejectBill>> page(@ApiParam(name="查询参数")String criteria, @ApiParam(name="每页条数")Integer pageSize, @ApiParam(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<RejectBill> rejectBills = service.listPage(RejectBill.class, page, c);
        return Result.OK(rejectBills);
    }

    @GetMapping("list/{noticeNo}")
    @ApiOperation(value = "根据noticeNo获取退货通知单", notes = "根据noticeNo获取退货通知单", httpMethod = "GET")
    public Result<RejectBill> getById(@ApiParam(name="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        RejectBill rejectBill = service.getById(RejectBill.class, noticeNo);
        return Result.OK(rejectBill);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增退货通知单", notes = "新增退货通知单", httpMethod = "POST")
    public Result<RejectBill> add(@RequestBody RejectBill rejectBill) {
        service.save(rejectBill);
        return Result.OK(rejectBill);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑退货通知单", notes = "编辑退货通知单", httpMethod = "PUT")
    public Result<RejectBill> edit(@RequestBody RejectBill rejectBill) {
        service.update(rejectBill);
        return Result.OK(rejectBill);
    }

    @DeleteMapping("remove/{noticeNos}")
    @ApiOperation(value = "根据noticeNo删除退货通知单", notes = "删除退货通知单", httpMethod = "DELETE")
    public Result<RejectBill> remove(@ApiParam(name="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "noticeNos", required = true) String noticeNos) {
        String[] noticeNo = noticeNos.split(",");
        for(String key : noticeNo){
            service.delete(RejectBill.class, key);
        }
        return Result.OK();
    }
}
